package com.scale.scales.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.scale.scales.R;
import com.scale.scales.bean.FoodAttributesBean;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class FoodAttriAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static HashMap<Integer, Boolean> isSelected;
    //数据集
    public List<FoodAttributesBean> mdatas;
    public Context context;
    private OnItemClickListener mOnItemClickListener;


    public FoodAttriAdapter(Context context, List<FoodAttributesBean> datas) {
        super();
        this.mdatas = datas;
        this.context = context;
    }


    public void notifyChange(List<FoodAttributesBean> datas) {
        this.mdatas = datas;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress_bar, parent, false);
        return new FoodViewHolder(view);


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        ((FoodAttriAdapter.FoodViewHolder) holder).show_zero.setVisibility(View.VISIBLE);
        ((FoodAttriAdapter.FoodViewHolder) holder).progress_no.setVisibility(View.VISIBLE);
        ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setVisibility(View.VISIBLE);
        ((FoodAttriAdapter.FoodViewHolder) holder).show_weight.setText("0");
        ((FoodAttriAdapter.FoodViewHolder) holder).show_zero.setText("0");


        int progressShow = mdatas.get(position).getProgressBar();
        if ("重量".equals(mdatas.get(position).getName())) {
            ((FoodAttriAdapter.FoodViewHolder) holder).show_zero.setVisibility(View.INVISIBLE);
            ((FoodAttriAdapter.FoodViewHolder) holder).show_weight.setText(mdatas.get(position).getWeightShow());
            ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setBackgroundColor(context.getResources().getColor(R.color.event_color_04));
        } else {

            ((FoodAttriAdapter.FoodViewHolder) holder).show_progress_text.setText(String.valueOf(progressShow) + "%");
            LinearLayout.LayoutParams params01 = (LinearLayout.LayoutParams) ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.getLayoutParams();
            LinearLayout.LayoutParams params02 = (LinearLayout.LayoutParams)((FoodAttriAdapter.FoodViewHolder) holder).progress_no.getLayoutParams();
            if (progressShow >= 100) {
                progressShow = 100;
                ((FoodAttriAdapter.FoodViewHolder) holder).progress_no.setVisibility(View.GONE);
            } else if (progressShow <= 0) {
                ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setVisibility(View.GONE);
            } else {
                params01.weight = progressShow;
                ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setLayoutParams(params01);
                params02.weight = 100 - progressShow;
                ((FoodAttriAdapter.FoodViewHolder) holder).progress_no.setLayoutParams(params02);
            }

            if (progressShow >= 50 && progressShow < 100) {
                ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setBackgroundColor(context.getResources().getColor(R.color.blue));
            } else if (progressShow >= 100) {
                ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setBackgroundColor(context.getResources().getColor(R.color.red));
            } else if (progressShow > 0 && progressShow < 50) {
                ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setBackgroundColor(context.getResources().getColor(R.color.green));
            } else {
                ((FoodAttriAdapter.FoodViewHolder) holder).show_progress.setBackgroundColor(context.getResources().getColor(R.color.event_color_04));
            }
        }
        ((FoodAttriAdapter.FoodViewHolder) holder).attr_name.setText(mdatas.get(position).getName());



    }

    @Override
    public int getItemCount() {
        return mdatas.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int positon);

        void onItemLongClick(View view, int positon);
    }

    public class FoodViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.show_percentage_linear)
        LinearLayout show_percentage_linear;
        @BindView(R.id.show_zero)
        TextView show_zero;
        @BindView(R.id.show_progress_text)
        TextView show_progress_text;
        @BindView(R.id.show_progress)
        LinearLayout show_progress;
        @BindView(R.id.show_weight)
        TextView show_weight;
        @BindView(R.id.progress_no)
        LinearLayout progress_no;
        @BindView(R.id.attr_name)
        TextView attr_name;
        public FoodViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}

